/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.resources
{
	import be.wellconsidered.apis.lighthouse.LightHouse;
	import be.wellconsidered.apis.lighthouse.connectors.*;
	import be.wellconsidered.apis.lighthouse.connectors.events.*;
	import be.wellconsidered.apis.lighthouse.resources.events.ProjectEvents;
	import be.wellconsidered.logging.Logger;
	
	public class Projects extends AbstractResource
	{
		private var _xmlNewProject:XML;
		
		public function Projects(lighthouseReference:LightHouse)
		{
			super(lighthouseReference);
		}
		
		/**
		 * List Projects
		 */
		public function listProjects():void
		{
			Logger.log("List Projects");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onProjectListingComplete, false, 0, true);
			
			function onProjectListingComplete(e:GETEvents):void
			{
				dispatchEvent(new ProjectEvents(ProjectEvents.LIST_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Get Project
		 */
		public function getProject(projectID:int):void
		{
			Logger.log("Get Project (" + projectID + ")");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + ".xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onProjectFetchComplete, false, 0, true);
			
			function onProjectFetchComplete(e:GETEvents):void
			{
				dispatchEvent(new ProjectEvents(ProjectEvents.GET_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * New Project
		 */
		public function newProject():void
		{
			Logger.log("New Project");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/new.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onNewProjectDataComplete, false, 0, true);
			
			function onNewProjectDataComplete(e:GETEvents):void
			{
				_xmlNewProject = new XML(e.data);
				
				dispatchEvent(new ProjectEvents(ProjectEvents.NEW_COMPLETE, false, false, _xmlNewProject));	
			}
		}
		
		/**
		 * Create Project
		 */
		public function createProject(projectName:String, isArchived:Boolean = false, isPublic:Boolean = false):void
		{
			Logger.log("Create Project");
			
			var xmlProject:XML = _xmlNewProject;
			
			xmlProject..archived = isArchived;
			xmlProject..name = projectName;
			xmlProject..public = isPublic;
			
			var connPOST:POST = new POST(_lhRef.getURL() + "projects.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlProject);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateProjectComplete, false, 0, true);
			
			function onCreateProjectComplete(e:POSTEvents):void
			{				
				dispatchEvent(new ProjectEvents(ProjectEvents.CREATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Update Project
		 */
		public function updateProject(projectXML:XML):void
		{
			Logger.log("Update Project (" + projectXML.id + ")");
			
			var connPUT:PUT = new PUT(_lhRef.getURL() + "projects/" + projectXML.id + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), projectXML);
			
			connPUT.addEventListener(PUTEvents.COMPLETE, onUpdateProjectComplete, false, 0, true);
			
			function onUpdateProjectComplete(e:PUTEvents):void
			{				
				dispatchEvent(new ProjectEvents(ProjectEvents.UPDATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Delete Project
		 */
		public function deleteProject(projectID:int):void
		{
			Logger.log("Delete Project (" + projectID + ")");
			
			var connDELETE:DELETE = new DELETE(_lhRef.getURL() + "projects/" + projectID + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding());
			
			connDELETE.addEventListener(DELETEEvents.COMPLETE, onDeleteProjectComplete, false, 0, true);
			
			function onDeleteProjectComplete(e:DELETEEvents):void
			{				
				dispatchEvent(new ProjectEvents(ProjectEvents.DELETE_COMPLETE, false, false, e.data));	
			}
		}
	}
}